const crypto = require('crypto');
// 解密函数
export function decryptDataForWechat(encryptedData, iv, session_key) {
  // base64解码
  const encrypted_data_buffer = Buffer.from(encryptedData, 'base64');
  const ivBuffer = Buffer.from(iv, 'base64');
  const session_key_buffer = Buffer.from(session_key, 'base64');

  // 创建解密器
  let decipher = crypto.createDecipheriv(
    'aes-128-cbc',
    session_key_buffer,
    ivBuffer
  );

  // 设置自动 padding 为 true
  decipher.setAutoPadding(true);

  // 解密
  let decrypted = decipher.update(
    encrypted_data_buffer,
    undefined,
    'utf8'
  );
  decrypted += decipher.final('utf8');
  return JSON.parse(decrypted);
}
